package cn.nc.service;


import cn.nc.entity.Article;
import cn.nc.entity.Comment;
import cn.nc.entity.User;
import cn.nc.repository.ArticleRepository;
import cn.nc.repository.CommentRepository;
import cn.nc.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    ArticleRepository articleRepository;

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    UserRepository userRepository;

    //查找文章
    public List<Comment> findCommentByArticleId(Integer id){
        return commentRepository.findByArticleId(id);
    }

    //新增文章
    public void addComment(Integer id,String author,String content){
        Comment comment = new Comment();
        Article article = articleRepository.findById(id).get();
        comment.setArticle(article);
        comment.setAuthor(author);
        comment.setContent(content);
        comment.setDate(new Date());
        commentRepository.save(comment);

    }



}
